﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using MicropleSoft.WmsServiceLibrary.ServiceFactory;
using MicropleSoft.WmsServiceLibrary.SqlServer.Base;
using MicropleSoft.WmsServiceLibrary.SqlServer.Factory;


namespace MicropleSoft.WmsServiceLibrary.Sequence
{
    public class SequenceGenerator :BaseServiceSqlServer, ISequenceGenerator
    {
        private readonly AbstractServiceFactory _abstractServiceFactory = ServiceFactoryCreator.CreateAbstractServiceFactory();
        public int GetMaxValue(string identityName)
        {
            var database = _abstractServiceFactory.CreateDatabase();
            using(DbConnection dbConnection =  database.CreateConnection() )
            {
                string a = dbConnection.ConnectionString;
                DbCommand command = dbConnection.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "ID_GENERATOR";
                DbParameter par = command.CreateParameter();
                par.DbType = DbType.String;
                par.ParameterName = "IdentityName";
                par.Value = identityName;
                command.Parameters.Add(par);

                par = command.CreateParameter();
                par.DbType = DbType.Int32;
                par.ParameterName = "ReturnValue";
                par.Direction = ParameterDirection.Output;
                command.Parameters.Add(par);

                dbConnection.Open();

                command.ExecuteNonQuery();

                dbConnection.Close();

                return (Int32) command.Parameters["ReturnValue"].Value;
            }
        }


        public int GetBillNumber(string numberName, string whId, int year, int month, int day)
        {
            var database = _abstractServiceFactory.CreateDatabase();
            using (DbConnection dbConnection = database.CreateConnection())
            {
                var command = dbConnection.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "NUMBER_GENERATOR";
                var par = command.CreateParameter();
                par.DbType = DbType.String;
                par.ParameterName = "NumberName";
                par.Value = numberName;
                command.Parameters.Add(par);

                par = command.CreateParameter();
                par.DbType = DbType.String;
                par.ParameterName = "WhId";
                par.Value = whId;
                command.Parameters.Add(par);


                par = command.CreateParameter();
                par.DbType = DbType.Int32;
                par.ParameterName = "NumberYear";
                par.Value = year;
                command.Parameters.Add(par);

                par = command.CreateParameter();
                par.DbType = DbType.Int32;
                par.ParameterName = "NumberMonth";
                par.Value = month;
                command.Parameters.Add(par);

                par = command.CreateParameter();
                par.DbType = DbType.Int32;
                par.ParameterName = "NumberDay";
                par.Value = day;
                command.Parameters.Add(par);

                par = command.CreateParameter();
                par.DbType = DbType.Int32;
                par.ParameterName = "ReturnValue";
                par.Direction = ParameterDirection.Output;
                command.Parameters.Add(par);

                dbConnection.Open();
                command.ExecuteNonQuery();
                dbConnection.Close();
                return (Int32) command.Parameters["ReturnValue"].Value;
            }
        }
    }
}
